var prefix = ctx + "manage/users";

var prefixUserDaily = ctx + "manage/userDailyActivity";
var prefixAntiCheatDetail = ctx + "manage/antiCheatDetail";

$(function () {
    var options = {
        url: prefix + "/list",
        queryParams: queryParams,
        columns: [
            {
                checkbox: true
            },
            {
                field: 'id',
                title: '用户ID'
            },
            {
                field: 'nickname',
                title: '用户名'
            },
            {
                field: 'usParentId',
                title: '推荐人ID'
            },
            {
                field: 'code',
                title: '邀请码'
            },
            {
                field: 'deviceId',
                title: '设备id'
            },
            {
                field: 'afbName',
                title: '姓名'
            },
            {
                field: 'afbPhone',
                title: '支付宝账号'
            },
            {
                field: 'moneyGoldCoin',
                title: '金币'
            },
            {
                field: 'moneyCommissionGoldCoin',
                title: '金币佣金'
            },
            {
                field: 'moneyRedPacket',
                title: '红包'
            },
            {
                field: 'moneyCommissionRedEnvelope',
                title: '红包佣金'
            },
            {
                field: 'moneyExtensionRedEnvelope',
                title: '推广佣金'
            },
            {
                field: 'signInNumber',
                title: '累计签到'
            },
            {
                field: 'signIdDayNumber',
                title: '连续签到'
            },
            {
                field: 'signIn',
                title: '今天签到',
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return '<span class="label label-warning">未签</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">已签</span>';
                    }
                }
            },
            {
                field: 'loginDayNumber',
                title: '累计登录'
            },
            {
                field: 'effective',
                title: '是否有效',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return '<span class="badge badge-primary">是</span>';
                    }
                    if (value === 2) {
                        return '<span class="badge badge-danger">否</span>';
                    }
                }
            },
            {
                field: 'loginInDayNumber',
                title: '连续登录'
            },
            {
                field: 'id',
                title: '登录日志',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<a class="btn btn-warning btn-sm" href="#" title="详细" onclick="queryUserDaily(\'' + value + '\')"><i>详细</i></a>');
                    return actions.join('');
                }
            },
            {
                field: 'status',
                title: '账号状态',
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return '<span class="badge badge-primary">正常用户</span>';
                    }
                    if (value === 2) {
                        return '<span class="badge badge-danger">违规封号</span>';
                    }
                }
            },
            {
                field: 'joinIp',
                title: '注册IP'
            },
            {
                field: 'createTime',
                title: '注册时间'
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    var actions = [];
                    actions.push('<div class="btn-group">\n' +
                        '    <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">\n' +
                        '      更多\n' +
                        '      <span class="caret"></span>\n' +
                        '    </button>\n' +
                        '    <ul class="dropdown-menu">\n' +
                        '      <li><a class="btn btn-xs ' + editFlag + '" href="#" onclick="edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>用户</a></li>\n' +
                        '      <li><a class="btn  btn-xs ' + resetKickbackFlag + '" href="#" onclick="resetKickback(\'' + row.id + '\')"><i class="fa fa-edit"></i>佣金</a></li>\n' +
                        '      <li><a class="btn  btn-xs ' + resetUsParentIdFlag + '" href="#" onclick="resetUsParentId(\'' + row.id + '\')"><i class="fa fa-edit"></i>推荐人</a></li>\n' +
                        '      <li><a class="btn  btn-xs ' + antiCheatDetailAdd + '" href="#" onclick="updateStatus(\'' + row.id + '\')"><i class="fa fa-edit"></i>封号</a></li>\n' +
                        '      <li><a class="btn  btn-xs ' + removeFlag + '" href="#" onclick="remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a></li>\n'
                    );
                    actions.push('</ul>');
                    actions.push('</div>');
                    return actions.join('');
                }
            }]
    };
    $.ryTable.init(options);

});

/*奖励记录-搜索*/
function search() {
    $('.bootstrap-table').bootstrapTable('refresh', queryParams);
}

function queryParams(params) {
    var createTime = $('#createTime').val();
    return {
        // 传递参数查询参数
        pageSize: params.limit,
        pageNum: params.offset / params.limit + 1,
        signIn: $("#signIn option:selected").val(),
        status: $("#status option:selected").val(),
        id: $("#usId").val(),
        usNickname: $("#usNickname").val(),
        usParentId: $("#usParentId").val(),
        afbPhone: $("#afbPhone").val(),
        startGoldCoin: $("#startGoldCoin").val(),
        endGoldCoin: $("#endGoldCoin").val(),
        startRedPacket: $("#startRedPacket").val(),
        endRedPacket: $("#endRedPacket").val(),
        startSignInNumber: $("#startSignInNumber").val(),
        endSignInNumber: $("#endSignInNumber").val(),
        startSignIdDayNumber: $("#startSignIdDayNumber").val(),
        endSignIdDayNumber: $("#endSignIdDayNumber").val(),
        startLoginDayNumber: $("#startLoginDayNumber").val(),
        endLoginDayNumber: $("#endLoginDayNumber").val(),
        startLoginInDayNumber: $("#startLoginInDayNumber").val(),
        endLoginInDayNumber: $("#endLoginInDayNumber").val(),
        effective: $("#effective").val(),
        startTime: createTime === '' ? '' : startCreatTime,
        endTime: createTime === '' ? '' : endTime
    };
}

/*用户账号-修改*/
function edit(id) {
    var url = prefix + '/edit/' + id;
    layer_showAuto("修改用户账号", url);
}

function resetKickback(id) {
    var url = prefix + '/kickback/' + id;
    layer_showAuto("修改佣金比例", url);
}

function resetUsParentId(id) {
    var url = prefix + '/usParentId/' + id;
    layer_showAuto("修改推荐人", url);
}

function queryUserDaily(id) {
    var url = prefixUserDaily + "/" + id;
    layer_showAuto("登录日志详细", url);
}

function updateStatus(id) {
    var url = prefixAntiCheatDetail + "/add/" + id;
    layer_showAuto("新增封号记录", url);
}

// 单条删除
function remove(id) {
    $.modalConfirm("确定要删除选中用户账号吗？", function () {
        _ajax(prefix + "/remove/" + id, "", "post");
    })
}

// 批量删除
function batchRemove() {
    var rows = $.getSelections("id");
    if (rows.length == 0) {
        $.modalMsg("请选择要删除的数据", "warning");
        return;
    }
    $.modalConfirm("确认要删除选中的" + rows.length + "条数据吗?", function () {
        _ajax(prefix + '/batchRemove', {"ids": rows}, "post");
    });
}
